//
//  HouseRentalRequestManager.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 16/9/13.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "HouseRentalRequestManager.h"

@implementation HouseRentalRequestManager
/**
 *  根据经纬度请求小区列表 可替换[[BusinessManager sharedManager].houseRentalEntityManager requestHouseRentalListWithLocation:
 *
 *  @param finished
 */
- (void)requestHouseRentalListWithLocation:(float)latitude withLongitude:(float)longitude withCommunityId:(NSString*)communityId withMinRent:(long)minRent withMaxRent:(long)maxRent withHall:(long)hall withRegionsId:(int)regionId Finished:(SEGNetworkRequestCallBack)finished
{
    
    NSMutableDictionary* dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:longitude], @"longitude", [NSNumber numberWithFloat:latitude], @"latitude", [NSNumber numberWithInt:HOUSERENTAL_LIST_NUMBER], @"pagination.pageSize", nil];
    if (communityId) {
        [dic setObject:[NSNumber numberWithInt:[communityId intValue]] forKey:@"communityId"];
    }
    if (minRent) {
        
        [dic setObject:[NSNumber numberWithLong:minRent] forKey:@"minRent"];
    }
    if (maxRent) {
        [dic setObject:[NSNumber numberWithLong:maxRent] forKey:@"maxRent"];
    }
    if (hall) {
        [dic setObject:[NSNumber numberWithLong:hall] forKey:@"room"];
    }
    if (regionId) {
        [dic setObject:[NSNumber numberWithInt:regionId] forKey:@"region"];
    }
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/rental/getRentalList.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:dic finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        
        NSString* code = [result objectForKey:@"code"];
        if (![code isKindOfClass:[NSString class]]) { //防止code为NSNull类型时崩溃
            code = @"-1";
        }
        NSString* message = [result objectForKey:@"message"];
        NSDictionary* data = [result objectForKey:@"data"];
        if ([data isKindOfClass:[NSNull class]]) { //防止data为NSNull类型时崩溃
            code = @"-1";
        }
        
        NSMutableDictionary* notifyDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:message, @"message", code, @"code", nil];
        
        if ([code isEqualToString:@"0"]) { //业务处理成功,开始解析业务字段
            NSArray* result = [data objectForKey:@"result"];
            
            //保存请求时间 请求更多时用到
            NSString* moreTime = [data objectForKey:@"requestTime"];
            if ([moreTime length] > 0) {
                //                [notifyDic setObject:moreTime forKey:@"TimestampMore"];
                self.requestTime = moreTime;
            }
            
            NSMutableArray* objectArray = [NSMutableArray array];
            for (NSDictionary* dic in result) {
                NSMutableDictionary* retDic = [NSMutableDictionary dictionary];
                if ([dic objectForKey:@"serviceId"]) {
                    [retDic setObject:[[dic objectForKey:@"serviceId"] stringValue] forKey:@"houseRentalID"];
                }
                if ([dic objectForKey:@"name"]) {
                    [retDic setObject:[dic objectForKey:@"name"] forKey:@"title"];
                }
                if ([dic objectForKey:@"communityAddress"]) {
                    [retDic setObject:[dic objectForKey:@"communityAddress"] forKey:@"address"];
                }
                if ([dic objectForKey:@"distance"]) {
                    [retDic setObject:[dic objectForKey:@"distance"] forKey:@"distance"];
                }
                if ([dic objectForKey:@"sellingPrice"]) {
                    [retDic setObject:[dic objectForKey:@"sellingPrice"] forKey:@"rentNumber"];
                }
                if ([dic objectForKey:@"description"]) {
                    [retDic setObject:[dic objectForKey:@"description"] forKey:@"desc"];
                }
                if ([dic objectForKey:@"updateTime"]) {
                    [retDic setObject:[dic objectForKey:@"updateTime"] forKey:@"updateTime"];
                }
                if ([dic objectForKey:@"pic1"]) {
                    [retDic setObject:[dic objectForKey:@"pic1"] forKey:@"pic1"];
                }
                if ([dic objectForKey:@"pic2"]) {
                    [retDic setObject:[dic objectForKey:@"pic2"] forKey:@"pic2"];
                }
                if ([dic objectForKey:@"pic3"]) {
                    [retDic setObject:[dic objectForKey:@"pic3"] forKey:@"pic3"];
                }
                if ([dic objectForKey:@"pic4"]) {
                    [retDic setObject:[dic objectForKey:@"pic4"] forKey:@"pic4"];
                }
                if ([dic objectForKey:@"pic5"]) {
                    [retDic setObject:[dic objectForKey:@"pic5"] forKey:@"pic5"];
                }
                
                [retDic setObjectExtra:[dic objectForKey:@"updateTime"] forKey:@"updateTime"];
                
                HouseModel* model = [HouseModel initWithDictionary:retDic];
                [objectArray addObject:model];
            }
            
            [notifyDic setObject:objectArray forKey:@"ObjectArray"];
            
            finished(notifyDic, nil);
            return;
        }
        else {
            finished(result, nil);
            return;
        }
    }];
}

/**
 *  根据经纬度请求更多小区列表 可替换[[BusinessManager sharedManager].houseRentalEntityManager requestMorHouseRentalListWithLocation:
 *
 *  @param finished
 */
- (void)requestMorHouseRentalListWithLocation:(float)latitude withLongitude:(float)longitude withIndex:(NSNumber*)index Finished:(SEGNetworkRequestCallBack)finished
{
    
    NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:longitude], @"longitude", [NSNumber numberWithFloat:latitude], @"latitude", [NSNumber numberWithInt:HOUSERENTAL_LIST_NUMBER], @"pagination.pageSize", index, @"pagination.index", self.requestTime, @"oldDate", nil];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/rental/getRentalMore.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:dic finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        NSString* code = [result objectForKey:@"code"];
        if (![code isKindOfClass:[NSString class]]) { //防止code为NSNull类型时崩溃
            code = @"-1";
        }
        NSString* message = [result objectForKey:@"message"];
        NSDictionary* data = [result objectForKey:@"data"];
        if ([data isKindOfClass:[NSNull class]]) { //防止data为NSNull类型时崩溃
            code = @"-1";
        }
        
        NSMutableDictionary* notifyDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:message, @"message", code, @"code", nil];
        
        if ([code isEqualToString:@"0"]) { //业务处理成功,开始解析业务字段
            NSArray* result = [data objectForKey:@"result"];
            
            //保存请求时间 请求更多时用到
            NSString* moreTime = [data objectForKey:@"requestTime"];
            if ([moreTime length] > 0) {
                //                [notifyDic setObject:moreTime forKey:@"TimestampMore"];
                self.requestTime = moreTime;
            }
            
            NSMutableArray* objectArray = [NSMutableArray array];
            for (NSDictionary* dic in result) {
                NSMutableDictionary* retDic = [NSMutableDictionary dictionary];
                if ([dic objectForKey:@"serviceId"]) {
                    [retDic setObject:[[dic objectForKey:@"serviceId"] stringValue] forKey:@"houseRentalID"];
                }
                if ([dic objectForKey:@"name"]) {
                    [retDic setObject:[dic objectForKey:@"name"] forKey:@"title"];
                }
                if ([dic objectForKey:@"brief"]) {
                    [retDic setObject:[dic objectForKey:@"brief"] forKey:@"address"];
                }
                if ([dic objectForKey:@"distance"]) {
                    [retDic setObject:[dic objectForKey:@"distance"] forKey:@"distance"];
                }
                if ([dic objectForKey:@"sellingPrice"]) {
                    [retDic setObject:[dic objectForKey:@"sellingPrice"] forKey:@"rentNumber"];
                }
                [retDic setObjectExtra:[dic objectForKey:@"updateTime"] forKey:@"updateTime"];
                
                HouseModel* model = [HouseModel initWithDictionary:retDic];
                [objectArray addObject:model];
            }
            
            [notifyDic setObject:objectArray forKey:@"ObjectArray"];
            
            finished(notifyDic, nil);
        }
        else {
            finished(result, nil);
        }
    }];
}

/**
 *  请求小区详情 可替换[[BusinessManager sharedManager].houseRentalEntityManager requestHouseRentalDetail:
 *
 *  @param finished
 */
- (void)requestHouseRentalDetailWithServiceId:(NSNumber*)serviceId Finished:(SEGNetworkRequestCallBack)finished
{
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/rental/getRentalDetail.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:@{@"serviceId":serviceId} finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        NSString* code = [result objectForKey:@"code"];
        if (![code isKindOfClass:[NSString class]]) { //防止code为NSNull类型时崩溃
            code = @"-1";
        }
        NSString* message = [result objectForKey:@"message"];
        NSDictionary* data = [result objectForKey:@"data"];
        if ([data isKindOfClass:[NSNull class]]) { //防止data为NSNull类型时崩溃
            code = @"-1";
        }
        
        NSMutableDictionary* notifyDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:message, @"message", code, @"code", nil];
        
        if ([code isEqualToString:@"0"]) { //业务处理成功,开始解析业务字段
            NSMutableDictionary* modelDic = [NSMutableDictionary dictionary];
            if ([data objectForKey:@"serviceId"]) {
                [modelDic setObject:[[data objectForKey:@"serviceId"] stringValue] forKey:@"houseRentalID"];
            }
            if ([data objectForKey:@"name"]) {
                [modelDic setObject:[data objectForKey:@"name"] forKey:@"title"];
            }
            if ([data objectForKey:@"description"]) {
                [modelDic setObject:[data objectForKey:@"description"] forKey:@"desc"];
            }
            if ([data objectForKey:@"communityId"]) {
                [modelDic setObject:[[data objectForKey:@"communityId"] stringValue] forKey:@"communityId"];
            }
            if ([data objectForKey:@"contact"]) {
                [modelDic setObject:[data objectForKey:@"contact"] forKey:@"contactName"];
            }
            if ([[data objectForKey:@"communityAttr"] objectForKey:@"officeTel"]) {
                [modelDic setObject:[[data objectForKey:@"communityAttr"] objectForKey:@"officeTel"] forKey:@"contactMobile"];
            }
            if ([data objectForKey:@"houseFloor"]) {
                [modelDic setObject:[data objectForKey:@"houseFloor"] forKey:@"floor"];
            }
            if ([data objectForKey:@"face"]) {
                [modelDic setObject:[data objectForKey:@"face"] forKey:@"face"];
            }
            if ([data objectForKey:@"houseArea"]) {
                [modelDic setObject:[data objectForKey:@"houseArea"] forKey:@"area"];
            }
            if ([data objectForKey:@"houseRoomInfo"]) {
                [modelDic setObject:[data objectForKey:@"houseRoomInfo"] forKey:@"bedroom"];
            }
            if ([data objectForKey:@"communityName"]) {
                [modelDic setObject:[data objectForKey:@"communityName"] forKey:@"communityName"];
            }
            if ([data objectForKey:@"communityAddress"]) {
                [modelDic setObject:[data objectForKey:@"communityAddress"] forKey:@"address"];
            }
            if ([data objectForKey:@"sellingPrice"]) {
                [modelDic setObject:[data objectForKey:@"sellingPrice"] forKey:@"rentNumber"];
            }
            if ([data objectForKey:@"updateTime"]) {
                [modelDic setObject:[data objectForKey:@"updateTime"] forKey:@"updateTime"];
            }
            if ([data objectForKey:@"pic1"]) {
                [modelDic setObject:[data objectForKey:@"pic1"] forKey:@"pic1"];
            }
            if ([data objectForKey:@"pic2"]) {
                [modelDic setObject:[data objectForKey:@"pic2"] forKey:@"pic2"];
            }
            if ([data objectForKey:@"pic3"]) {
                [modelDic setObject:[data objectForKey:@"pic3"] forKey:@"pic3"];
            }
            if ([data objectForKey:@"pic4"]) {
                [modelDic setObject:[data objectForKey:@"pic4"] forKey:@"pic4"];
            }
            if ([data objectForKey:@"pic5"]) {
                [modelDic setObject:[data objectForKey:@"pic5"] forKey:@"pic5"];
            }
            if ([[data allKeys] containsObject:@"tel"] && [[data objectForKey:@"tel"] length] > 0) {
                [modelDic setObject:[data objectForKey:@"tel"] forKey:@"contactMobile"];
            }else if([[data allKeys] containsObject:@"communityAttr"] && [[data objectForKey:@"communityAttr"] isKindOfClass:[NSDictionary class]] ){
                NSDictionary *communityAttr = [data objectForKey:@"communityAttr"];
                if([[communityAttr allKeys] containsObject:@"customTel"] && [[communityAttr objectForKey:@"customTel"] length] > 0){
                    [modelDic setObject:[communityAttr objectForKey:@"customTel"] forKey:@"contactMobile"];
                }else if([[communityAttr allKeys] containsObject:@"officeTel"] && [[communityAttr objectForKey:@"officeTel"] length] > 0){
                    [modelDic setObject:[communityAttr objectForKey:@"officeTel"] forKey:@"contactMobile"];
                }
            }
            /*
             else
             */
            [modelDic setObjectExtra:[data objectForKey:@"houseId"] forKey:@"houseId"];
            
            HouseModel* model = [HouseModel initWithDictionary:modelDic];
            [notifyDic setObject:model forKey:@"model"];
            finished(notifyDic, nil);
        }
        else {
            finished(result, nil);
        }
    }];
}

/**
 *  请求预约求租 可替换[[BusinessManager sharedManager].houseRentalEntityManager uploadAppoinDataToService:
 *
 *  @param finished
 */
- (void)requestHouseRentaApponitWithServiceId:(NSNumber*)serviceId withContact:(NSString*)contactStr withTel:(NSString*)telStr orDescription:(NSString*)description Finished:(SEGNetworkRequestCallBack)finished
{
    
    NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:serviceId, @"serviceId", contactStr, @"contact", telStr, @"tel", description, @"description", nil];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/rental/appoint.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:dict finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        NSString* code = [result objectForKey:@"code"];
        if (![code isKindOfClass:[NSString class]]) { //防止code为NSNull类型时崩溃
            code = @"-1";
        }
        
        NSDictionary* data = [result objectForKey:@"data"];
        if ([data isKindOfClass:[NSNull class]]) { //防止data为NSNull类型时崩溃
            code = @"-1";
        }
        finished(result, nil);
    }];
}

/**
 *  请求城市信息 可替换[[ServiceInterface sharedServiceInterface] findCityInfo];
 *
 *  @param finished
 */
- (void)requestCityInfo:(SEGNetworkRequestCallBack)finished
{
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/common/getRegions.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:nil finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        NSString* code = [result objectForKey:@"code"];
        NSString* message = [result objectForKey:@"message"];
        
        NSMutableDictionary* notifyDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:message, @"message", code, @"code", nil];
        
        if ([code isEqualToString:@"0"]) { //业务处理成功,开始解析业务字段
            NSArray* resultArr = [result objectForKey:@"data"];
            NSMutableArray* nameList = [NSMutableArray arrayWithCapacity:resultArr.count];
            NSMutableArray* secNameList = [NSMutableArray arrayWithCapacity:resultArr.count];
            
            for (NSDictionary* aDic in resultArr) {
                
                [nameList addObject:[aDic objectForKey:@"name"]];
                NSArray* regions = [aDic objectForKey:@"regions"];
                NSMutableArray* nameList2 = [NSMutableArray arrayWithCapacity:regions.count];
                for (NSDictionary* dic in regions) {
                    NSMutableDictionary* regionsDic = [NSMutableDictionary dictionaryWithCapacity:2];
                    [regionsDic setObject:[dic objectForKey:@"regionId"] forKey:@"regionId"];
                    [regionsDic setObject:[dic objectForKey:@"name"] forKey:@"name"];
                    [nameList2 addObject:regionsDic];
                }
                [secNameList addObject:nameList2];
            }
            
            [notifyDic setObjectExtra:nameList forKey:@"nameList"];
            [notifyDic setObjectExtra:secNameList forKey:@"secNameList"];
        }
        finished(notifyDic, nil);
    }];
}

/**
 *  请求我的求租 可替换[[BusinessManager sharedManager].houseRentalEntityManager requestHouseRentalMyAppoint::
 *
 *  @param finished
 */
- (void)requestHouseRentalMyAppointWithUserId:(NSString*)userId Finished:(SEGNetworkRequestCallBack)finished
{
    NSString *houseRentaMyApponit = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/rental/getMyAppointmentList.json?",nil];
    NSString* urlStr = [NSString stringWithFormat:@"%@userId=%@",houseRentaMyApponit, userId];
    
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlStr parameters:nil finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        NSString* code = [result objectForKey:@"code"];
        if (![code isKindOfClass:[NSString class]]) { //防止code为NSNull类型时崩溃
            code = @"-1";
        }
        NSString* message = [result objectForKey:@"message"];
        NSDictionary* data = [result objectForKey:@"data"];
        if ([data isKindOfClass:[NSNull class]]) { //防止data为NSNull类型时崩溃
            code = @"-1";
        }
        
        NSMutableDictionary* notifyDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:message, @"message", code, @"code", nil];
        
        if ([code isEqualToString:@"0"]) { //业务处理成功,开始解析业务字段
            NSMutableArray* objectArray = [NSMutableArray array];
            for (NSDictionary* dic in data) {
                NSMutableDictionary* retDic = [NSMutableDictionary dictionary];
                if ([dic objectForKey:@"serviceId"]) {
                    [retDic setObject:[[dic objectForKey:@"serviceId"] stringValue] forKey:@"houseRentalID"];
                }
                if ([dic objectForKey:@"hold1"]) {
                    [retDic setObject:[dic objectForKey:@"hold1"] forKey:@"title"];
                }
                if ([dic objectForKey:@"communityAddress"]) {
                    [retDic setObject:[dic objectForKey:@"communityAddress"] forKey:@"address"];
                }
                if ([dic objectForKey:@"distance"]) {
                    [retDic setObject:[dic objectForKey:@"distance"] forKey:@"distance"];
                }
                if ([dic objectForKey:@"totalPrice"]) {
                    [retDic setObject:[dic objectForKey:@"totalPrice"] forKey:@"rentNumber"];
                }
                if ([dic objectForKey:@"status"]) {
                    [retDic setObject:[dic objectForKey:@"status"] forKey:@"houseRentalState"];
                }
                if ([dic objectForKey:@"applyTime"]) {
                    [retDic setObject:[dic objectForKey:@"applyTime"] forKey:@"updateTime"];
                }
                if ([dic objectForKey:@"hold2"]) {
                    [retDic setObject:[dic objectForKey:@"hold2"] forKey:@"pic1"];
                }
                if ([dic objectForKey:@"description"]) {
                    [retDic setObject:[dic objectForKey:@"description"] forKey:@"description"];
                }
                
                HouseModel* model = [HouseModel initWithDictionary:retDic];
                [objectArray addObject:model];
            }
            
            [notifyDic setObject:objectArray forKey:@"ObjectArray"];
        }
        
        finished(notifyDic, nil);
    }];
}

/**
 *  请求发布租房信息 可替换[[BusinessManager sharedManager].houseRentalEntityManager addHouseRentalEntityData:
 *
 *  @param finished
 */
- (void)requestAddHouseRentalWithModel:(HouseModel*)model withPhotos:(NSArray*)photosArray Finished:(SEGNetworkReponse)finished
{
    if ([photosArray count] > 0) { //有图像需要上传
        
        [[SEGMultipartfileuploadManager sharedManager] requestMultipartfileuploadWithImage:photosArray progress:nil completed:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            if (!isSuccessed) {
                finished(NO,nil, @"-1",@"上传照片失败,请重新提交");
                return;
            }

            NSArray *data = [result objectForKey:@"data"];
            if ([data isKindOfClass:[NSArray class]] && data.count <= 0) {
                finished(NO,nil, @"-1",@"上传照片失败,请重新提交");
                return;
            }
            
            [self segAddHouseRentalWithModel:model imageUrls:data Finished:finished];
        }];
    } else {
        [self segAddHouseRentalWithModel:model imageUrls:nil Finished:finished];
    }
}

-(void)segAddHouseRentalWithModel:(HouseModel*)model imageUrls:(NSArray*)imageUrls Finished:(SEGNetworkReponse)finished{
    
    HouseModel* entity = model;
    
    NSMutableDictionary* requsetInfo = nil;
    NSString* name = entity.title;
    NSNumber* houseId = [NSNumber numberWithLong:[entity.houseId integerValue]];
    NSNumber* sellingPrice = entity.rentNumber;
    NSString* description = entity.desc;
    NSString* floor = entity.floor;
    NSString* face = entity.face;
    NSString* contact = entity.contactName;
    NSString* tel = entity.contactMobile;
    NSNumber* issuePerson = [NSNumber numberWithInt:[kAccountUserID intValue]];
    
    requsetInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:name, @"name", houseId, @"houseId", sellingPrice, @"sellingPrice", contact, @"contact", tel, @"tel", issuePerson, @"issuePerson", nil];
    if (description) {
        [requsetInfo setValue:description forKey:@"description"];
    }
    [requsetInfo setValue:floor forKey:@"houseFloor"];
    [requsetInfo setValue:face forKey:@"face"];
    
    NSString* houseInfo = entity.bedroom;
    houseInfo = [houseInfo stringByReplacingOccurrencesOfString:@"室" withString:@"/"];
    houseInfo = [houseInfo stringByReplacingOccurrencesOfString:@"厅" withString:@"/"];
    houseInfo = [houseInfo stringByReplacingOccurrencesOfString:@"卫" withString:@"/"];
    NSArray* infoArray = [houseInfo componentsSeparatedByString:@"/"];
    NSString* room = [infoArray objectAtIndex:0];
    NSString* hall = [infoArray objectAtIndex:1];
    NSString* toilet = [infoArray objectAtIndex:2];
    
    [requsetInfo setValue:room forKey:@"room"];
    [requsetInfo setValue:hall forKey:@"hall"];
    [requsetInfo setValue:toilet forKey:@"toilet"];
    [requsetInfo setValue:entity.area forKey:@"houseArea"];
    
    [requsetInfo setValue:entity.houseRentalID forKey:@"serviceId"];
    
    if (imageUrls) {
        NSArray* imageArray = [NSArray arrayWithArray:imageUrls];
        for (int i = 0; i < imageArray.count; i++) {
            [requsetInfo setValue:imageArray[i] forKey:[NSString stringWithFormat:@"pic%d", i + 1]];
        }
    }
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/rental/saveRental.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:requsetInfo finished:finished];
}

/**
 *  请求我的放盘 可替换[[BusinessManager sharedManager].houseRentalEntityManager requestHouseRentalMySale:
 *
 *  @param finished
 */
- (void)requestHouseRentalMySaleWithUserId:(NSString*)userId Finished:(SEGNetworkRequestCallBack)finished
{
    NSString *houseRentaMySale = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/rental/getUserRentalList.json?",nil];
    NSString* urlStr = [NSString stringWithFormat:@"%@userId=%@",houseRentaMySale, userId];
    
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlStr parameters:nil finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        NSString* code = [result objectForKey:@"code"];
        if (![code isKindOfClass:[NSString class]]) { //防止code为NSNull类型时崩溃
            code = @"-1";
        }
        NSString* message = [result objectForKey:@"message"];
        NSDictionary* data = [result objectForKey:@"data"];
        if ([data isKindOfClass:[NSNull class]]) { //防止data为NSNull类型时崩溃
            code = @"-1";
        }
        
        NSMutableDictionary* notifyDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:message, @"message", code, @"code", nil];
        
        if ([code isEqualToString:@"0"]) { //业务处理成功,开始解析业务字段
            NSMutableArray* objectArray = [NSMutableArray array];
            for (NSDictionary* dic in data) {
                
                NSMutableDictionary* retDic = [NSMutableDictionary dictionary];
                if ([dic objectForKey:@"serviceId"]) {
                    [retDic setObject:[[dic objectForKey:@"serviceId"] stringValue] forKey:@"houseRentalID"];
                }
                if ([dic objectForKey:@"name"]) {
                    [retDic setObject:[dic objectForKey:@"name"] forKey:@"title"];
                }
                if ([dic objectForKey:@"communityAddress"]) {
                    [retDic setObject:[dic objectForKey:@"communityAddress"] forKey:@"address"];
                }
                if ([dic objectForKey:@"distance"]) {
                    [retDic setObject:[dic objectForKey:@"distance"] forKey:@"distance"];
                }
                if ([dic objectForKey:@"sellingPrice"]) {
                    [retDic setObject:[dic objectForKey:@"sellingPrice"] forKey:@"rentNumber"];
                }
                if ([dic objectForKey:@"status"]) {
                    [retDic setObject:[dic objectForKey:@"status"] forKey:@"houseRentalState"];
                }
                if ([dic objectForKey:@"updateTime"]) {
                    [retDic setObject:[dic objectForKey:@"updateTime"] forKey:@"updateTime"];
                }
                if ([dic objectForKey:@"tel"]) {
                    [retDic setObject:[dic objectForKey:@"tel"] forKey:@"contactMobile"];
                }
                if ([dic objectForKey:@"contact"]) {
                    [retDic setObject:[dic objectForKey:@"contact"] forKey:@"contactName"];
                }
                if ([dic objectForKey:@"pic1"]) {
                    [retDic setObject:[dic objectForKey:@"pic1"] forKey:@"pic1"];
                }
                if ([dic objectForKey:@"pic2"]) {
                    [retDic setObject:[dic objectForKey:@"pic2"] forKey:@"pic2"];
                }
                if ([dic objectForKey:@"pic3"]) {
                    [retDic setObject:[dic objectForKey:@"pic3"] forKey:@"pic3"];
                }
                if ([dic objectForKey:@"pic4"]) {
                    [retDic setObject:[dic objectForKey:@"pic4"] forKey:@"pic4"];
                }
                if ([dic objectForKey:@"pic5"]) {
                    [retDic setObject:[dic objectForKey:@"pic5"] forKey:@"pic5"];
                }
                if ([dic objectForKey:@"description"]) {
                    [retDic setObject:[dic objectForKey:@"description"] forKey:@"description"];
                }
                
                HouseModel* model = [HouseModel initWithDictionary:retDic];
                [objectArray addObject:model];
            }
            
            [notifyDic setObject:objectArray forKey:@"ObjectArray"];
        }
        
        finished(notifyDic, nil);
    }];
}

/**
 *  请求删除我的放盘 可替换[[BusinessManager sharedManager].houseRentalEntityManager delHouseRentalEntityData:
 *
 *  @param finished
 */
- (void)requestdeleteHouseRentalDataWithServiceId:(NSString*)serviceId Finished:(SEGNetworkRequestCallBack)finished
{
    
    NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:serviceId, @"serviceId", nil];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/rental/delete.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:dict finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        NSString* code = [result objectForKey:@"code"];
        if (![code isKindOfClass:[NSString class]]) { //防止code为NSNull类型时崩溃
            code = @"-1";
        }
        
        NSDictionary* data = [result objectForKey:@"data"];
        if ([data isKindOfClass:[NSNull class]]) { //防止data为NSNull类型时崩溃
            code = @"-1";
        }
        
        finished(result, nil);
    }];
}

/**
 *  请求更新租房信息 可替换[[BusinessManager sharedManager].houseRentalEntityManager updateHouseRentalEntityData:
 *
 *  @param finished
 */
- (void)requestUpdateHouseRentalWithModel:(HouseModel*)model withPhotos:(NSArray*)photosArray isEditPhoto:(BOOL)isEditPhoto Finished:(SEGNetworkReponse)finished
{
    if (isEditPhoto && [photosArray count] > 0) { //有图像需要上传
        [[SEGMultipartfileuploadManager sharedManager] requestMultipartfileuploadWithData:photosArray progress:nil completed:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            if (!isSuccessed) {
                finished(NO,nil, @"-1",@"上传照片失败,请重新提交");
                return;
            }
            
            NSArray *data = [result objectForKey:@"data"];
            if ([data isKindOfClass:[NSArray class]] && data.count <= 0) {
                finished(NO,nil, @"-1",@"上传照片失败,请重新提交");
                return;
            }
            
            [self segUpdateHouseRentalWithModel:model withPhotos:data isEditPhoto:isEditPhoto Finished:finished];
        }];
    }else{
        [self segUpdateHouseRentalWithModel:model withPhotos:nil isEditPhoto:isEditPhoto Finished:finished];
    }
    
    
}

-(void)segUpdateHouseRentalWithModel:(HouseModel*)model withPhotos:(NSArray*)photosArray isEditPhoto:(BOOL)isEditPhoto Finished:(SEGNetworkReponse)finished{
    
    HouseModel* entity = model;
    
    NSMutableDictionary* requsetInfo = nil;
    NSString* name = entity.title;
    NSNumber* houseId = [NSNumber numberWithLong:[entity.houseId integerValue]];
    NSNumber* sellingPrice = entity.rentNumber;
    NSString* description = entity.desc;
    NSString* floor = entity.floor;
    NSString* face = entity.face;
    NSString* contact = entity.contactName;
    NSString* tel = entity.contactMobile;
    NSNumber* issuePerson = [NSNumber numberWithInt:[kAccountUserID intValue]];
    
    requsetInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:name, @"name", houseId, @"houseId", sellingPrice, @"sellingPrice", contact, @"contact", tel, @"tel", issuePerson, @"issuePerson", nil];
    if (description) {
        [requsetInfo setValue:description forKey:@"description"];
    }
    [requsetInfo setValue:floor forKey:@"houseFloor"];
    [requsetInfo setValue:face forKey:@"face"];
    
    NSString* houseInfo = entity.bedroom;
    houseInfo = [houseInfo stringByReplacingOccurrencesOfString:@"室" withString:@"/"];
    houseInfo = [houseInfo stringByReplacingOccurrencesOfString:@"厅" withString:@"/"];
    houseInfo = [houseInfo stringByReplacingOccurrencesOfString:@"卫" withString:@"/"];
    NSArray* infoArray = [houseInfo componentsSeparatedByString:@"/"];
    NSString* room = [infoArray objectAtIndex:0];
    NSString* hall = [infoArray objectAtIndex:1];
    NSString* toilet = [infoArray objectAtIndex:2];
    
    [requsetInfo setValue:room forKey:@"room"];
    [requsetInfo setValue:hall forKey:@"hall"];
    [requsetInfo setValue:toilet forKey:@"toilet"];
    [requsetInfo setValue:entity.area forKey:@"houseArea"];
    
    [requsetInfo setValue:ValueToString(entity.houseRentalID) forKey:@"serviceId"];
    
    if (isEditPhoto) {
        NSArray* imageArray = [NSArray arrayWithArray:photosArray];
        for (int i = 0; i < 5; i++) {
            NSString *imageURL = @"";
            if (i < imageArray.count) {
                imageURL = imageArray[i];
            }
            [requsetInfo setValue:imageURL forKey:[NSString stringWithFormat:@"pic%d", i + 1]];
        }
    } else {
        // 没有编辑图片的时候，参数和之前保持一致
        requsetInfo[@"pic1"] = ValueToString(model.pic1);
        requsetInfo[@"pic2"] = ValueToString(model.pic2);
        requsetInfo[@"pic3"] = ValueToString(model.pic3);
        requsetInfo[@"pic4"] = ValueToString(model.pic4);
        requsetInfo[@"pic5"] = ValueToString(model.pic5);
    }
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/rental/update.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:requsetInfo finished:finished];
}

+ (void)requesFindDictionaryInfoWithCode:(NSString *)code finished:(SEGNetworkRequestCallBack)finished {
    
    // 3. 网路请求方法(必须添加)
    [[SEGFindDictionaryInfoManagr sharedManager] requestDictionaryInfoByCode:code completed:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
        if (!isSuccessed) {
            finished(result,nil);
            return ;
        }
        
        NSDictionary *responseDic = result;
        NSString *code = [responseDic objectForKey:@"code"];
        NSString *message = [responseDic objectForKey:@"message"];
        NSDictionary *data = [responseDic objectForKey:@"data"];
        
        NSMutableDictionary* notifyDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:message,@"message",code,@"code",nil];
        
        if([code isEqualToString:@"0"])
        {
            NSArray *result=[data objectForKey:@"result"];
            NSMutableArray *nameList = [NSMutableArray arrayWithCapacity:result.count];
            NSMutableArray *valueList = [NSMutableArray arrayWithCapacity:result.count];
            
            for (NSDictionary* aDic in result) {
                [nameList addObject:[aDic valueForKey:@"name"]];
                [valueList addObject:[aDic valueForKey:@"value"]];
                
            }
            
            [notifyDic setObjectExtra:nameList  forKey:@"nameList"];
            [notifyDic setObjectExtra:valueList  forKey:@"valueList"];
            
        }
        finished(notifyDic,nil);
    }];
}
@end

